﻿package com.wandian.view.mediator
{
	import com.greensock.TweenLite;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.controller.CornucopiaCommand;
	import com.wandian.data.GameData;
	import com.wandian.model.UserInfoProxy;
	import com.wandian.model.vo.PlayerVo;
	import com.wandian.util.ButtonUtil;
	import com.wandian.util.CLZButton;
	import com.wandian.util.CLZButtonFour;
	import com.wandian.util.CLZTabBar;
	import com.wandian.util.CLZTiming;
	import com.wandian.util.GlowFilterManage;
	import com.wandian.util.LoadIconUtil;
	import com.wandian.util.NumRollText;
	import com.wandian.util.Res;
	import com.wandian.util.UIHelper;
	import com.wandian.view.components.ui.SetPanel;
	import com.wandian.view.components.ui.SoundManage;
	import com.wandian.view.mediator.HelpMediator;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.ui.Keyboard;
	
	import ghostcat.display.transfer.effect.BombHandler;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class TopMediator extends Mediator
	{
		private var _global:GameData;
		/**
		 * 个人的金钱
		 * @default
		 */
		private var numRollMoney:NumRollText;
		private var numRollGold:NumRollText;
		/**
		 *积宝
		 * @default
		 */
		private var numRollPot:NumRollText;
		private var _filter:GlowFilterManage;
		private var _mcSetPanel:Sprite;
		private var _setPanel:SetPanel;
		private var _userInfo:UserInfoProxy;
		public static const NAME:String="TopMediator";
		private static var offsetPerSecond:Number;
		private static var toCornucopia:Number;
		private static var updateCornucopiaSecond:int;
		private static var curUpdateCornucopiacount:int;
		/**
		 * 因为CLZTime是全局唯一的，一次只能Tween一个,为了保证积宝数字的独立tween，需单独定义一个对象
		 * @default 
		 */
		private static var potTween:TweenLite;

		public function TopMediator(viewComponent:Object=null)
		{
			this._global=GameData.getInstance();
			this._filter=GlowFilterManage.getInstance();
			super(NAME, viewComponent);
			return;
		}

		override public function onRegister():void
		{
			_userInfo=facade.retrieveProxy(UserInfoProxy.NAME) as UserInfoProxy
			facade.registerCommand(CLZNoticeName.SHOW_CORNUCOPIA_PANEL, CornucopiaCommand);
			this.mcTop.gamePot_mc.mouseChildren=false;
			this.mcTop.gamePot_mc.buttonMode=true;
			this.mcTop.gamePot_mc.addEventListener(MouseEvent.CLICK, this.onGamePotHandler);
//			this.numRollMoney=new NumRollText(this.mcTop["money_txt"], this._global.userInfo.userVo.money, 3);
			this.numRollMoney=new NumRollText(this.mcTop["gold_txt"], this._global.userInfo.userVo.money, 5);
			this.numRollPot=new NumRollText(this.mcTop.gamePot_mc.txt, 0, 1, GameData.tip["GOLD"]);
			this.sendNotification(CLZNoticeName.UPDATE_MONEY, this._global.userInfo.userVo.money);
			this.sendNotification(CLZNoticeName.UPDATE_GOLD, this._global.userInfo.userVo.money);
//			this.mcTop.honor_btn.addEventListener(MouseEvent.CLICK, this.onHonorHandler);
			this.mcTop.honor_btn.visible=false;
			this.mcTop.message_btn.addEventListener(MouseEvent.CLICK, this.onMessageHandler);
			CLZButtonFour.setButton(this.mcTop.set_btn);
			this.mcTop.set_btn.addEventListener(MouseEvent.CLICK, this.onSetHandler);
			CLZButtonFour.setButton(this.mcTop.full_btn);
			this.mcTop.full_btn.addEventListener(MouseEvent.CLICK, this.onFullScreenHandler);
			Texas.I._hotkey.registerGlobalKey(Keyboard.ESCAPE, this.initFullBtnHandler);
//			this.mcTop.moneyName_txt.visible=false;		
			this.mcTop.moneyName_txt.text=this._userInfo.userVo.name;
			this.mcTop["money_txt"].visible=false
//			this.mcTop.goldName_txt.text=this._userInfo.userVo.name;

//			this.mcTop.honorName_txt.text="成就:";
			this.mcTop.honorName_txt.visible=false;
//			this.mcTop.pay_txt.htmlText="<a href=\'event:pay\'>获取金币</a>";
//			this.mcTop.pay_txt.addEventListener(TextEvent.LINK, this.linkTxtHandler);
			this.mcTop.pay_txt.visible=false;
			CLZButton.setButton(this.mcTop.message_btn);
			CLZButton.setButton(this.mcTop.message_btn, "", {tip: "消息中心", time: 500, pointXY: {disX: 0, disY: 25}});
			CLZButton.setButton(this.mcTop.help_btn, "帮助");
			this.mcTop.help_btn.addEventListener(MouseEvent.CLICK, this.onHelpHandler);
			this.mcTop.gamePot_mc.name_txt.text="积宝";
//			this.mcTop.gamePot_mc.visible=false;
			return;
		}

		private function onHelpHandler(event:MouseEvent):void
		{
			this.sendNotification(CLZNoticeName.HELPCOMMAND_SHOW_HELP, HelpMediator.TYPE_1);
			return;
		}

		private function linkTxtHandler(event:TextEvent):void
		{
			this.sendNotification(CLZNoticeName.ON_PAY_VIP);
			return;
		}

		private function onHonorHandler(event:MouseEvent):void
		{
			var _loc_2:*=new PlayerVo();
			_loc_2=GameData.copyClass(this._global.userInfo.userVo, _loc_2) as PlayerVo;
			this.sendNotification(CLZNoticeName.ACHIEVEMENT_BTN, _loc_2);
			return;
		}

		private function onMessageHandler(event:MouseEvent):void
		{
			this.mcTop.message_btn.filters=this._filter.getGrayscale();
			this.sendNotification(CLZNoticeName.SHOW_GAME_MESSAGE_PANEL, GameData.SYSTEM_MESSAGE);
			return;
		}

		private function onSetHandler(event:MouseEvent):void
		{
			this.sendNotification(CLZNoticeName.SHOW_SET_PANEL);
			return;
		}

		override public function listNotificationInterests():Array
		{
			return [CLZNoticeName.UPDATE_MONEY, CLZNoticeName.LOGINMEDIATOR_RES_LOGIN, CLZNoticeName.UPDATE_GOLD, CLZNoticeName.UPDATE_LEVEL, CLZNoticeName.SHOW_TOP, CLZNoticeName.HIDE_TOP, CLZNoticeName.HIDE_CORNUCOPIA_PANEL, CLZNoticeName.UPDATE_CORNUCOPIA_GOLD, CLZNoticeName.UPDATE_HONOR, CLZNoticeName.UPDATE_RANK_USER, CLZNoticeName.SHOW_SET_PANEL, CLZNoticeName.HIDE_SET_PANEL];
		}

		override public function handleNotification(notification:INotification):void
		{
			var arrHonor:Array=null;
			switch (notification.getName())
			{
				case CLZNoticeName.SHOW_TOP:
				{
					this.mcTop.visible=true;
					break;
				}
				case CLZNoticeName.LOGINMEDIATOR_RES_LOGIN:
				{
					this.mcTop.goldName_txt.text=this._userInfo.userVo.name;
					break;
				}
				case CLZNoticeName.HIDE_TOP:
				{
					this.mcTop.visible=false;
					break;
				}
				case CLZNoticeName.UPDATE_MONEY:
				{
					this.updateMoney(notification.getBody() as Number);
					break;
				}
				case CLZNoticeName.UPDATE_GOLD:
				{
					this.updateGold(notification.getBody() as uint);
					break;
				}
				case CLZNoticeName.UPDATE_LEVEL:
				{
					this.updateLevel(notification.getBody() as uint);
					break;
				}
				case CLZNoticeName.HIDE_CORNUCOPIA_PANEL:
				{
					this.mcTop.gamePot_mc.addEventListener(MouseEvent.CLICK, this.onGamePotHandler);
					break;
				}
				case CLZNoticeName.UPDATE_CORNUCOPIA_GOLD:
				{
					var objData:Object=notification.getBody();
					this.numRollPot.num=objData.toCornucopia - objData.offsetCornucopia;
					toCornucopia=objData.toCornucopia;
//					CLZTiming.remove(this.updateMoneyPerSecond);
					if (potTween != null)
					{
						potTween.kill();
						potTween=null;
					}
					curUpdateCornucopiacount=0;
					//打开定时器去加
					if (objData.offsetCornucopia > 0)
					{
						offsetPerSecond=objData.offsetCornucopia / objData.updateCornucopiaSecond;
						updateCornucopiaSecond=objData.updateCornucopiaSecond;
//						CLZTiming.add(1, this.updateMoneyPerSecond);
						potTween=TweenLite.delayedCall(1,this.updateMoneyPerSecond);
					}
					else
					{
						offsetPerSecond=0;
					}
					break;
				}
				case CLZNoticeName.UPDATE_HONOR:
				{
					arrHonor=notification.getBody() as Array;
					this.mcTop.honor_txt.text=arrHonor[0].length + arrHonor[1].length + arrHonor[2].length + "/" + GameData.honorXml.honor.item.length();
					break;
				}
				case CLZNoticeName.UPDATE_RANK_USER:
				{
					this.mcTop.rank_txt.htmlText=GameData.replaceStrValue("您在全球排第 {font color='#00FF00'}%NUM%{/font} 名", "NUM", this._global.userInfo.userVo.rank);
					break;
				}
				case CLZNoticeName.SHOW_SET_PANEL:
				{
					if (this._mcSetPanel != null)
					{
						this.sendNotification(CLZNoticeName.HIDE_SET_PANEL);
					}
					else
					{
						this._mcSetPanel=GameData.getSprite("SetPanel");
						Texas.I._mcWindosView.addChildTo(this._mcSetPanel);
						Texas.I._mcWindosView.centerXY(this._mcSetPanel);
						this._setPanel=new SetPanel(this._mcSetPanel);
					}
					break;
				}
				case CLZNoticeName.HIDE_SET_PANEL:
				{
					this.sendNotification(CLZNoticeName.INIT_SET, SoundManage.volume * 100);
					Texas.I._mcWindosView.removeChildTo(this._mcSetPanel);
					this._mcSetPanel=null;
					this._setPanel=null;
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		public function updateMoneyPerSecond():void
		{
			if (curUpdateCornucopiacount >= updateCornucopiaSecond)
			{
//				CLZTiming.remove(this.updateMoneyPerSecond);
				potTween.kill();
				potTween=null;
				this.numRollPot.num=toCornucopia;
				return;
			}
			this.numRollPot.num=this.numRollPot.num + offsetPerSecond;
			curUpdateCornucopiacount++;
//			CLZTiming.add(1, this.updateMoneyPerSecond);
			potTween=TweenLite.delayedCall(1,this.updateMoneyPerSecond);
		}

		private function updateMoney(money:Number):void
		{
			this.numRollMoney.num=money;
			trace(money, "updateMoney");
			return;
		}

		private function updateGold(param1:uint):void
		{
//			trace(param1, "updateGold");
//			this.numRollGold.num=param1;
			return;
		}

		private function updateLevel(param1:uint):void
		{
			return;
		}

		private function onGamePotHandler(event:MouseEvent):void
		{
			this.mcTop.gamePot_mc.removeEventListener(MouseEvent.CLICK, this.onGamePotHandler);
			this.sendNotification(CLZNoticeName.SHOW_CORNUCOPIA_PANEL);
			return;
		}

		private function onFullScreenHandler(event:MouseEvent):void
		{
			switch (Texas.I._mcGameView.stage.displayState)
			{
				case "normal":
				{
					Texas.I._mcGameView.stage.displayState="fullScreen";
					break;
				}
				case "fullScreen":
				{
					Texas.I._mcGameView.stage.displayState="normal";
					break;
				}
				default:
				{
					break;
				}
			}
			return;
		}

		private function initFullBtnHandler():void
		{
			CLZButtonFour.initBtn(this.mcTop.full_btn, false);
			return;
		}

		private function get mcTop():MovieClip
		{
			return viewComponent as MovieClip;
		}

	}
}
